import React from 'react'
import { connect } from 'dva'
import { mapStateToProps } from 'utils/view_utils'
import { CollapsiblePanels } from 'widget/CollapsiblePanels/index'
import PopUpSelect from 'widget/PopUpSelectnoModal/PopUpSelect'
import { isEmpty, cloneDeep, } from 'lodash'
import cx from 'classnames'
import Uploader from 'widget/Upload'
import locale from 'antd/lib/date-picker/locale/zh_CN';
import { Form, Input, Select, Checkbox , Row, Col, Button, DatePicker, Radio  } from 'antd';
// import { message,DatePicker} from 'antd';
import moment from 'moment';
//导入路由组件，用于跳转等
import { Link, hashHistory } from 'dva/router'
import 'moment/locale/zh-cn';
import { number } from 'prop-types'
moment.locale('zh-cn');
const FormItem = Form.Item;
const { RangePicker } = DatePicker;
const { Option } = Select
const routeView = function ({ dispatch, model, form }) {
    const { modelObj, namespace } = model
    const { getFieldDecorator } = form
    let { title, type, query, detail, actionType, images, outerImage, houseTypeImage, devices } = modelObj
    //console.log('selectItem',selectItem)
    // PopUpSelect 需要row配置
    const formItemLayout = {
        labelCol: {
            span: 1,

        },
        wrapperCol: {
            span: 16
        },
    }
    const imageLayout = {
        labelCol: { span: 2 },
        wrapperCol: { span: 22 },
    }
    function removeImgUrl(inx, origin){
        images.splice(inx ,1)
        dispatch({
            type: `${namespace}/updateStore`,
            payload: { images }
        })
    }
    function updateImageModel(url, origin){
        images.push(url)
        dispatch({
            type: `${namespace}/updateStore`,
            payload: { images }
        })
    }

    function updateKey(value, key){
        dispatch({
            type: `${namespace}/updateStore`,
            payload: { key: value}
        })
    }

    function updateModel(value, key, obj){
        detail[key] = value
        dispatch({
            type: `${namespace}/updateStore`,
            payload: { detail }
        })
    }
    //选择角色

    

    //date改变时
    return (
        <div className={cx('public_listMain')}>
            <div className="functionButton">
                {actionType == 'check' ? (<Button type='primary' loading={modelObj.btnLoading} onClick={() => {
                    hashHistory.push('/home/businessAdmin/lifeServiceCategory')
                    
                }} >返回</Button>): <Button type='primary' loading={modelObj.btnLoading} onClick={() => {
                    form.validateFields((err) => {
                        if(!err){
                            if (actionType == 'add'){
                                dispatch({
                                    type: `${namespace}/createLifeServiceCategory`,
                                    payload: {detail}
                                })
                            }else if(actionType == 'edit'){
                                dispatch({
                                    type: `${namespace}/updateLifeServiceCategory`,
                                    payload: {detail}
                                })
                            }
                        }
                    });

                }} >保存</Button> }
            </div>
            <CollapsiblePanels title={title} hideUpDownBtn={true}  >
                <Form autoComplete="off" className="auto-wrap" >

                    <Row style={{ borderBottom: "1px solid #EFF0F2" }}>
                        <Col span={8}>
                            <FormItem {...formItemLayout} label="分类名称" labelCol={{ span: 5 }}>
                                <div>
                                    {getFieldDecorator('category', {
                                        initialValue: detail.category,
                                        rules: [
                                            { required: true, whitespace: true, message: '请输入分类名称' },
                                        ],
                                        })(<Input
                                        placeholder={'请输入分类名称'}
                                        autoComplete={'off'}
                                        disabled={actionType == 'check'}
                                        maxLength={50}
                                        onChange={(e) => { updateModel(e.target.value, 'category', 'detail') }}
                                    />)}
                                </div>
                            </FormItem>
                        </Col>
                        <Col span={8}>
                            <FormItem {...formItemLayout} label="排序" labelCol={{ span: 5 }}>
                                <div>
                                    {getFieldDecorator('sort', {
                                        initialValue: detail.sort,
                                        rules: [
                                            { required: true, whitespace: true, message: '请输入排序' },
                                        ],
                                        })(<Input
                                        placeholder={'请输入排序'}
                                        autoComplete={'off'}
                                        disabled={actionType == 'check'}
                                        maxLength={100}
                                        onChange={(e) => { updateModel(e.target.value, 'sort', 'detail') }}
                                    />)}
                                </div>
                            </FormItem>
                        </Col>
                        
                    </Row>                    
                    <Row style={{ borderBottom: "1px solid #EFF0F2" }}>

                        <Col span={8}>
                            <FormItem {...formItemLayout} label="是否推荐" labelCol={{ span: 5 }} >
                                <div className={cx('select-item-contain')}>
                                    {getFieldDecorator('recommend', {
                                        initialValue: detail.recommend,
                                        rules: [
                                            { required: true, whitespace: true, message: '请选择',pattern: new RegExp(/^[a-zA-Z0-9_]+$/) },
                                        ],
                                        })(<Radio.Group  value={detail.recommend} disabled={actionType == 'check'} onChange={(e) => { updateModel(e.target.value, 'recommend', 'detail') }}>
                                            <Radio value={0}>否</Radio>
                                            <Radio value={1}>是</Radio>
                                            
                                          </Radio.Group>)}
                                </div>
                            </FormItem>
                        </Col>
                    </Row>
                    
                    <Row style={{ borderBottom: "1px solid #EFF0F2" }}>
                    <Col span={8}>
                        
                    <FormItem {...imageLayout} label="图标"
                                    style={{ marginLeft: "8.33333333%" }}>
                            <Uploader uploadedUrls ={images}
                                        removeFileCbf={(index)=>{removeImgUrl(index,'images')}}
                                        uploadSuccessCbf={ (imgUrl) => {updateImageModel(imgUrl, 'images')} }
                                        showType={actionType == 'check'?'4':'3'}
                                        fileType='image'
                                        fileMaxSize='0.5'
                                        minCropBoxWidth={1500}
                                        minCropBoxHeight={3000}
                                        maxFiles='1' />
                        </FormItem>
                    </Col>
                </Row>
                </Form>
            </CollapsiblePanels>
        </div>

    )


}
export default connect(mapStateToProps)(Form.create()(routeView))

